package cn.com.server.service.impl;

import cn.com.server.dao.TerminalDao;
import cn.com.server.dao.UserDao;
import cn.com.server.dao.UserRoleDao;
import cn.com.server.entity.Terminal;
import cn.com.server.entity.User;
import cn.com.server.entity.UserRole;
import cn.com.server.service.UserService;
import cn.com.server.extend.model.RegisterSaveModel;
import cn.com.server.extend.view.UserView;
import cn.com.server.extend.model.UserViewModel;

import java.sql.SQLDataException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author qing
 * @since 2017-11-20
 */
@Service
public class UserServiceImp implements UserService {
	
	@Autowired
	private UserDao dao;
	
	@Autowired
	private UserRoleDao userRoleDao;
	
	@Autowired
	private TerminalDao terminalDao;

	@Override
	public User selectByName(String name) {
		return dao.selectByName(name);
	}
	
	@Override
	public UserView selectById(Long id) {
		return dao.selectById(id);
	}
	
	@Override
	@Transactional
	public int insert(User entity) {
		return dao.insert(entity);
	}
	
	@Override
	@Transactional
	public int update(User entity) {
		return dao.update(entity);
	}
	
	/**
	 * 仅终端组人员新增使用
	 */
	@Override
	@Transactional
	public int insertUserAndTerminalAndUserRole(RegisterSaveModel model) throws SQLDataException {
		User user = new User();
		user.setName(model.getName());
		user.setPassword(model.getPassword());
		if(model.getNickname()==null||"".equals(model.getNickname())) {
			model.setNickname(model.getName());
		}
		user.setNickname(model.getNickname());
		if(dao.insert(user)>0) {
			UserRole userRole = new UserRole();
			userRole.setRoleId(model.getRoleId());
			userRole.setUserId(user.getId());
			if(userRoleDao.insert(userRole)<1) {
				throw new SQLDataException();
			}
			Terminal terminal = new Terminal();
			terminal.setIp(model.getIp());
			terminal.setMac(model.getMac());
			terminal.setName(model.getTmname());
			terminal.setUserName(model.getName());
			terminal.setUserId(user.getId());
			terminal.setOnline(0);
			if(terminalDao.insert(terminal)<1) {
				throw new SQLDataException();
			}
		}else {
			throw new SQLDataException();
		}
		return 1;
	}
	
	/**
	 * 仅终端组人员更新使用
	 */
	@Override
	@Transactional
	public int updateUserAndTerminalAndUserRole(RegisterSaveModel model) throws SQLDataException {
		User user = new User();
		user.setId(model.getId());
		user.setStatus(model.getStatus());
		user.setName(model.getName());
		user.setPassword(model.getPassword());
		if(model.getNickname()==null||"".equals(model.getNickname())) {
			model.setNickname(model.getName());
		}
		user.setNickname(model.getNickname());
		dao.update(user);
		UserRole userRole = new UserRole();
		userRole.setRoleId(model.getRoleId());
		userRole.setUserId(user.getId());
		userRole.setStatus(model.getStatus());
		userRoleDao.update(userRole);
		Terminal terminal = new Terminal();
		terminal.setIp(model.getIp());
		terminal.setMac(model.getMac());
		terminal.setName(model.getTmname());
		terminal.setUserName(model.getName());
		terminal.setUserId(user.getId());
		terminal.setStatus(model.getStatus());
		if(model.getStatus()==0) {
			terminal.setOnline(0);
		}
		terminalDao.update(terminal);
		return 1;
	}
	
	/**
	 * 仅WEB管理员组人员新建使用
	 */
	@Override
	@Transactional
	public int insertUserAndUserRole(RegisterSaveModel model) throws SQLDataException {
		User user = new User();
		user.setName(model.getName());
		user.setPassword(model.getPassword());
		if(model.getNickname()==null||"".equals(model.getNickname())) {
			model.setNickname(model.getName());
		}
		user.setNickname(model.getNickname());
		if(dao.insert(user)>0) {
			UserRole userRole = new UserRole();
			userRole.setRoleId(model.getRoleId());
			userRole.setUserId(user.getId());
			if(userRoleDao.insert(userRole)<1) {
				throw new SQLDataException();
			}
		}else {
			throw new SQLDataException();
		}
		return 1;
	}
	
	/**
	 * 仅WEB管理员组人员更新使用
	 */
	@Override
	@Transactional
	public int updateUserAndUserRole(RegisterSaveModel model){
		User user = new User();
		user.setId(model.getId());
		user.setStatus(model.getStatus());
		user.setName(model.getName());
		user.setPassword(model.getPassword());
		if(model.getNickname()==null||"".equals(model.getNickname())) {
			model.setNickname(model.getName());
		}
		user.setNickname(model.getNickname());
		dao.update(user);
		UserRole userRole = new UserRole();
		userRole.setRoleId(model.getRoleId());
		userRole.setUserId(user.getId());
		userRole.setStatus(model.getStatus());
		userRoleDao.update(userRole);
		return 1;
	}
	
	@Override
	public Page<UserView> selectUserViewList(UserViewModel model){
		PageHelper.startPage(model.getPage(), model.getLimit());
		Page<UserView> pageData = dao.selectUserViewList(model);
		return pageData;
	}
	
	@Override
	@Transactional
	public int deleteUpdateUserAndUserRoleAndTerminal(String ids) throws SQLDataException {
		String [] idList = ids.split(",");
		int count = 0;
		if(idList!=null&&idList.length>0) {
			RegisterSaveModel registerSaveModel = new RegisterSaveModel();
			for(String id:idList) {
				if(id!=null&&!"".equals(id)) {
					try {
						registerSaveModel.setId(Long.valueOf(id));
						registerSaveModel.setStatus(0);
						count = count + updateUserAndTerminalAndUserRole(registerSaveModel);
					} catch (Exception e) {
						throw new SQLDataException();
					}
				}
			}
		}
		return count;
	}
	
}
